package org.sixtynine.stock.service.content;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.sixtynine.stock.Constants.CommonData;
import org.sixtynine.stock.entity.BaseEntity;
import org.sixtynine.stock.entity.Company;
import org.sixtynine.stock.entity.Module;
import org.sixtynine.stock.entity.Sector;
import org.sixtynine.stock.entity.SubSector;
import org.sixtynine.stock.entity.UserCategory;
import org.sixtynine.stock.service.GenericService;
import org.sixtynine.stock.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Thilanka
 * 
 */
@Component
public class DataManagerService implements ManagerService {

	private static final Logger logger = Logger
			.getLogger(DataManagerService.class);

	@Autowired
	private GenericService genericService;

	public static Map<String, List<BaseEntity>> dataMap = new HashMap<String, List<BaseEntity>>();
	public static Map<String, HashMap<Object, BaseEntity>> mapById = new HashMap<String, HashMap<Object, BaseEntity>>();

	@Override
	@PostConstruct
	public void init() {
		logger.info("################### Initailize the DataManager Service ####################");
		List<BaseEntity> companyList = genericService.findAll(Company.class);
		if (companyList != null && companyList.size() > 0) {
			mapData(CommonData.COMPANY.toString(), companyList);
		}

		List<BaseEntity> sectorList = genericService.findAll(Sector.class);
		if (sectorList != null && sectorList.size() > 0) {
			mapData(CommonData.SECTOR.toString(), sectorList);
		}

		List<BaseEntity> subSectorList = genericService
				.findAll(SubSector.class);
		if (subSectorList != null && subSectorList.size() > 0) {
			mapData(CommonData.SUB_SECTOR.toString(), subSectorList);
		}

		List<BaseEntity> userCategoryList = genericService
				.findAll(UserCategory.class);
		if (userCategoryList != null && userCategoryList.size() > 0) {
			mapData(CommonData.USER_CATEGORY.toString(), userCategoryList);
		}

		List<BaseEntity> moduleList = genericService.findAll(Module.class);
		if (moduleList != null && moduleList.size() > 0) {
			mapData(CommonData.MODULE.toString(), moduleList);
		}

	}

	/**
	 * when new data is added, load it again
	 * 
	 * @param param
	 * @param beanClass
	 */
	public void loadData(String param, Class beanClass) {
		List<BaseEntity> beanList = genericService.findAll(beanClass);
		if (beanList != null && !beanList.isEmpty()) {
			mapData(param, beanList);
		}
	}

	private static void reArrange(String code, List<BaseEntity> data) {
		HashMap<Object, BaseEntity> idMap = null;
		if (mapById.containsKey(code)) {
			idMap = mapById.get(code);
		}
		if (data != null) {
			if (idMap == null) {
				idMap = new HashMap<Object, BaseEntity>();
			}
			for (BaseEntity bean : data) {
				idMap.put(bean._getKey(), bean);
			}

			mapById.put(code, idMap);
		}
	}

	private static void mapData(String code, List<BaseEntity> data) {
		dataMap.put(code, data);
		reArrange(code, data);
	}

	public static List<BaseEntity> getData(String param) {
		if (dataMap.containsKey(param)) {
			return dataMap.get(param);
		}
		return null;
	}

	/**
	 * @param param
	 * @return get the data as a map with a key
	 */
	public static HashMap<Object, BaseEntity> getDataMap(String param) {
		if (mapById.containsKey(param)) {
			return mapById.get(param);
		}

		return null;
	}

	public static BaseEntity getData(String param, Object id) {
		if (mapById.containsKey(param)) {
			HashMap<Object, BaseEntity> idMap = mapById.get(param);
			if (idMap.containsKey(id)) {
				return idMap.get(id);
			}
		}
		return null;
	}

}
